package com.academic_system.service.impl;

import com.academic_system.mapper.StudentDao;
import com.academic_system.mapper.WarningDao;
import com.academic_system.model.MarkCondition;
import com.academic_system.model.Warning;
import com.academic_system.service.WarningService;
import com.academic_system.util.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 20155884 王晓阳
 * @className : WarningImpl
 * @Description
 **/
@Service

public class WarningImpl implements WarningService {
    @Autowired
    private StudentDao studentDao;
    @Autowired
    @Resource
    private WarningDao warningDao;

    /**
     * @param markCondition
     * @Description 动态从Warning表查找学业警示
     * @author 20155884 王晓阳
     * @methodName selectWarning
     * @returnType String
     **/
    @Override
    public Message selectWarning(MarkCondition markCondition) {
        List<MarkCondition> warningList = warningDao.selectWarning(markCondition);
        for (MarkCondition w : warningList) {
            System.out.println("----------学号：" + w.getStuInfoId() + "  学业警告个数：" + w.getWarningNum() + "id" + w.getWarningId()+"" +
                    "******************");
        }
        if (warningList.size() != 0) {
            return Message.success().add("warningList", warningList);
        }
        return Message.fail();
    }

    /**
     * @param warning
     * @Description 给所修学分不足总学分一半的学生学业警告数置1，即添加学业警告
     * @author 20155884 王晓阳
     * @methodName addWarning
     * @returnType int
     **/
    @Override
    public Message addWarning(Warning warning) {
        int flag = warningDao.addWarning(warning);
        if (flag == 1) {
            return Message.success().add("100", "成功");
        }
        return Message.fail();
    }

    /**
     * @param markCondition
     * @Description 查询学生的总学分
     * @author 20155884 王晓阳
     * @methodName findStuCredithour
     * @returnType Msg
     **/
    @Override
    public Message findStuCredithour1(MarkCondition markCondition) {
        MarkCondition markCondition1 = new MarkCondition();
        Warning selectWarning = new Warning();
        List<MarkCondition> markConditionList = new ArrayList<MarkCondition>();

        //获取所有学生
        List<MarkCondition> studentList = studentDao.queryAllStudent();
        System.out.println("stulist,size:" + studentList.size());
        //遍历学生，查找每个学生的学分
        for (MarkCondition s : studentList) {
            System.out.println("stuid" + s.getStuInfoId());
            markCondition.setStuInfoId(s.getStuInfoId());
            //调用查询每个学生实际得到的学分
            markCondition1 = warningDao.findStuCredithour1(markCondition);

            //获取应得总学分
            int actualCredithour = warningDao.findTotalCourseCredithour(markCondition.getTermId());
            markCondition1.setActualCredithour(actualCredithour);
            System.out.println("***************actualCredithour=" + actualCredithour);
            System.out.println("***************dui对象=" + markCondition1.toString());
            System.out.println("------------stustustustustus"+markCondition1.getStuCourseCresithour());

            //查询学业警示表中num的个数
            Warning warning = new Warning();
            warning.setStuInfoId(s.getStuInfoId());
            warning.setTermId(markCondition.getTermId());
            selectWarning = warningDao.selectWarningByIds(warning);
            //判断查询出的warningNum是否为空，如果不为空就将取得的值赋给markCondition1,如果为空就赋0
            if (selectWarning != null) {
                markCondition1.setWarningNum(selectWarning.getWarningNum());
            } else {
                markCondition1.setWarningNum(0);
            }
            System.out.println("sssss---" + markCondition1.toString());
            //判断学生所修学分是否够总学分的一半，如果小于实际的，就将要获得学业警示的学生存入list
            if ((markCondition1.getStuCourseCresithour() * 2 )< actualCredithour) {
                markConditionList.add(markCondition1);
            }
        }
        System.out.println("mclist大小：" + markConditionList.size() + "---" + markConditionList.toString());
        return Message.success().add("markConditionList", markConditionList);
    }

    /**
     * @param term_id
     * @Description 在courseterm表中按学期查询本学期所有课程的总学分
     * @author 20155884 王晓阳
     * @methodName findTotalCourseCredithour
     * @returnType Msg
     **/
    @Override
    public int findTotalCourseCredithour(int term_id) {
        Integer totalCourseCredithour = warningDao.findTotalCourseCredithour(term_id);
        System.out.println("--------------本学期所有学分总计：" + totalCourseCredithour);
       /* if (totalCourseCredithour != null) {
            return Msg.addData(100, "成功", totalCourseCredithour);
        }
        return Msg.fail();*/
        return totalCourseCredithour;
    }

    /**
     * @param warningId
     * @Description 删除学业警示
     * @author 20155884 王晓阳
     * @methodName deleteWarning
     * @returnType
     **/
    public Message deleteWarning(int warningId) {
        int flag = warningDao.deleteWarning(warningId);
        if (flag == 1) {
            return Message.success();
        }
        return Message.fail().add("100", "删除失败");
    }
}
